package o;

import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.downloadengine.impl.DownloadTask;
import com.huawei.appgallery.downloadengine.impl.DownloadThreadInfo;
import com.huawei.appgallery.downloadengine.impl.SplitDownloadThreadInfo;
import java.util.Collection;
import java.util.List;

class bnt {
    private static bnt ˊ = null;
    private static final Object ˎ = new Object();
    private cfu ˋ;
    private cfu ˏ;
    private cfu ॱ;

    public static bnt ˋ() {
        bnt o_bnt;
        synchronized (ˎ) {
            if (ˊ == null) {
                ˊ = new bnt();
            }
            o_bnt = ˊ;
        }
        return o_bnt;
    }

    private bnt() {
        bnn ˎ = bnn.ˎ();
        this.ˏ = ˎ.ʽ("SessionDownloadTask");
        this.ॱ = ˎ.ʽ(SplitTask.TABLE_NAME);
        this.ˋ = ˎ.ʽ(SplitDownloadThreadInfo.TABLE_NAME);
    }

    public void ॱ(SessionDownloadTask sessionDownloadTask) {
        bnc.ˎ.ˋ("SessionDownloadDAO", "SessionDownloadDAO deleteTask, package=" + sessionDownloadTask.ˊˊ());
        this.ˏ.ˋ("sessionId_=?", new String[]{String.valueOf(sessionDownloadTask.ʾ())});
        this.ॱ.ˋ("sessionId_=?", new String[]{String.valueOf(sessionDownloadTask.ʾ())});
        this.ˋ.ˋ("sessionId_=?", new String[]{String.valueOf(sessionDownloadTask.ʾ())});
    }

    public void ॱ(long j, long j2) {
        this.ˋ.ˋ("sessionId_=? and splitId_=?", new String[]{String.valueOf(j), String.valueOf(j2)});
    }

    public void ˋ(SessionDownloadTask sessionDownloadTask) {
        if (sessionDownloadTask.ˊˊ() != null) {
            bnc.ˎ.ˋ("SessionDownloadDAO", "SessionDownloadDAO insertTask, package=" + sessionDownloadTask.ˊˊ());
            ॱ(sessionDownloadTask.ʾ());
            this.ˏ.ˎ((cfr) sessionDownloadTask);
            for (cfr o_cfr : sessionDownloadTask.ᐝˊ()) {
                this.ॱ.ˎ(o_cfr);
                for (cfr o_cfr2 : o_cfr2.ˎ()) {
                    this.ˋ.ˎ(o_cfr2);
                }
            }
        }
    }

    private void ॱ(long j) {
        for (SessionDownloadTask ॱ : this.ˏ.ˋ(SessionDownloadTask.class, "sessionId_=?", new String[]{String.valueOf(j)}, null, null)) {
            ॱ(ॱ);
        }
    }

    public void ˋ(SplitTask splitTask) {
        if (this.ॱ.ˋ(SplitTask.class, "sessionId_=? and splitId_=?", new String[]{String.valueOf(splitTask.ˉ()), String.valueOf(splitTask.ˊˋ())}, null, null).isEmpty()) {
            this.ॱ.ˎ((cfr) splitTask);
            return;
        }
        this.ॱ.ॱ(splitTask, "sessionId_=? and splitId_=?", new String[]{String.valueOf(splitTask.ˉ()), String.valueOf(splitTask.ˊˋ())});
    }

    private void ॱ(SplitDownloadThreadInfo splitDownloadThreadInfo) {
        if (this.ˋ.ˋ(SplitDownloadThreadInfo.class, "sessionId_=? and splitId_=? and threadIndexId_=?", new String[]{String.valueOf(splitDownloadThreadInfo.ˋ()), String.valueOf(splitDownloadThreadInfo.ˊ()), String.valueOf(splitDownloadThreadInfo.ॱ())}, null, null).isEmpty()) {
            this.ˋ.ˎ((cfr) splitDownloadThreadInfo);
            return;
        }
        this.ˋ.ॱ(splitDownloadThreadInfo, "sessionId_=? and splitId_=? and threadIndexId_=?", new String[]{String.valueOf(splitDownloadThreadInfo.ˋ()), String.valueOf(splitDownloadThreadInfo.ˊ()), String.valueOf(splitDownloadThreadInfo.ॱ())});
    }

    public void ˊ(SessionDownloadTask sessionDownloadTask) {
        if (this.ˏ.ॱ(sessionDownloadTask, "sessionId_=?", new String[]{String.valueOf(sessionDownloadTask.ʾ())}) > 0) {
            for (SplitTask splitTask : sessionDownloadTask.ᐝˊ()) {
                ˋ(splitTask);
                for (SplitDownloadThreadInfo ॱ : splitTask.ˎ()) {
                    ॱ(ॱ);
                }
            }
            return;
        }
        bnc.ˎ.ˊ("HiAppDownload", "SessionDownloadDAO updateTask failed");
    }

    private List<SplitTask> ˊ(long j) {
        return this.ॱ.ˋ(SplitTask.class, "sessionId_=?", new String[]{String.valueOf(j)}, null, null);
    }

    private List<SplitDownloadThreadInfo> ˊ(long j, long j2) {
        return this.ˋ.ˋ(SplitDownloadThreadInfo.class, "sessionId_=? and splitId_=?", new String[]{String.valueOf(j), String.valueOf(j2)}, null, null);
    }

    public List<SessionDownloadTask> ˊ() {
        bnc.ˎ.ˋ("SessionDownloadDAO", "SessionDownloadDAO getAllTask");
        List<SessionDownloadTask> ˋ = this.ˏ.ˋ(SessionDownloadTask.class, null);
        bnc.ˎ.ˋ("HiAppDownload", "get all task  size=" + ˋ.size());
        for (SessionDownloadTask sessionDownloadTask : ˋ) {
            Collection<SplitTask> ˊ = ˊ(sessionDownloadTask.ʾ());
            for (SplitTask splitTask : ˊ) {
                Collection ˊ2 = ˊ(splitTask.ˉ(), splitTask.ˊˋ());
                splitTask.ˎ().clear();
                splitTask.ˎ().addAll(ˊ2);
            }
            sessionDownloadTask.ᐝˊ().clear();
            sessionDownloadTask.ᐝˊ().addAll(ˊ);
        }
        return ˋ;
    }

    public void ॱ() {
        try {
            bnc.ˎ.ˋ("HiAppDownload", "begin restoreTaskFromOldVersion");
            if (bnn.ˎ().ॱ(bng.ˏ)) {
                List<DownloadTask> ˋ = bng.ˏ().ˋ();
                bnc.ˎ.ˋ("HiAppDownload", "task size = " + ˋ.size());
                for (DownloadTask ˋ2 : ˋ) {
                    ˋ().ˋ(ˋ(ˋ2));
                }
                bnn.ˎ().ˏ(bng.ˏ);
                bnn.ˎ().ˏ(bng.ॱ);
            }
        } catch (Exception e) {
            bnc.ˎ.ˊ("HiAppDownload", "SessionDownloadDAO restoreTaskFromOldVersion exception :" + e.getMessage());
        }
    }

    private SessionDownloadTask ˋ(DownloadTask downloadTask) {
        SessionDownloadTask sessionDownloadTask = new SessionDownloadTask();
        sessionDownloadTask.ˋॱ(downloadTask.ˏˏ());
        sessionDownloadTask.ˏ(downloadTask.ˊˊ());
        sessionDownloadTask.ˏ(downloadTask.ˋˊ());
        sessionDownloadTask.ॱ(downloadTask.ॱᐝ());
        sessionDownloadTask.ˎ(downloadTask.ʻ());
        sessionDownloadTask.ॱˊ(0);
        sessionDownloadTask.ˋॱ(downloadTask.ˌ());
        sessionDownloadTask.ˏ(downloadTask.ͺ());
        sessionDownloadTask.ͺ(downloadTask.ॱʽ());
        sessionDownloadTask.ॱˊ(downloadTask.ـ());
        sessionDownloadTask.ͺ(downloadTask.ˑ());
        sessionDownloadTask.ʽ(downloadTask.ˎˎ());
        sessionDownloadTask.ˏॱ(downloadTask.ˏˎ());
        sessionDownloadTask.ˏ(downloadTask.ˏॱ());
        sessionDownloadTask.ˋ(downloadTask.ॱॱ());
        sessionDownloadTask.ʼ(downloadTask.ˊॱ());
        sessionDownloadTask.ॱ(downloadTask.ॱ());
        sessionDownloadTask.ॱॱ(downloadTask.ʼॱ());
        sessionDownloadTask.ʼ(downloadTask.ˊᐝ());
        sessionDownloadTask.ʽ(downloadTask.ʾ());
        sessionDownloadTask.ˊॱ(downloadTask.ͺॱ());
        sessionDownloadTask.ˊ(downloadTask.ˎ());
        sessionDownloadTask.ˎ((long) downloadTask.ʿ());
        sessionDownloadTask.ˊ(downloadTask.ʼ());
        sessionDownloadTask.a_(downloadTask.ˎˏ());
        sessionDownloadTask.ॱॱ(downloadTask.ˈ());
        sessionDownloadTask.ᐝ(downloadTask.ʻॱ());
        sessionDownloadTask.ᐝ(downloadTask.ˋᐝ());
        sessionDownloadTask.ॱ(downloadTask.ˊˋ());
        sessionDownloadTask.ˊ(downloadTask.ˊ());
        sessionDownloadTask.ˊ(downloadTask.ʽॱ());
        sessionDownloadTask.ʻ(downloadTask.ᐝॱ());
        sessionDownloadTask.ˋ(downloadTask.ʽ());
        sessionDownloadTask.ˎ(downloadTask.ᐝ());
        SplitTask splitTask = new SplitTask();
        splitTask.ॱ(2);
        splitTask.ˏॱ(downloadTask.ʽॱ());
        splitTask.ˎ(downloadTask.ˊˋ());
        splitTask.ˋॱ(downloadTask.ॱˋ());
        splitTask.ˋ(downloadTask.ॱʽ());
        splitTask.ͺ(downloadTask.ˉ());
        splitTask.ˎ(0);
        splitTask.ˊॱ(downloadTask.ˊᐝ());
        splitTask.ˏ((long) downloadTask.ʿ());
        splitTask.ˊ(downloadTask.ॱˋ());
        splitTask.ˋ(downloadTask.ˊˋ());
        splitTask.ʼ(downloadTask.ˎˏ());
        splitTask.ˏ((long) downloadTask.ʿ());
        splitTask.ᐝ((long) downloadTask.ʿ());
        splitTask.ˊ(downloadTask.ˋˊ());
        splitTask.ˋ(downloadTask.ʽॱ());
        if (downloadTask.ˋˋ() != 0 && downloadTask.ˊˋ() == downloadTask.ˋˋ()) {
            splitTask.ॱ(1);
            splitTask.ˎ(downloadTask.ˋˋ());
            splitTask.ˋॱ(downloadTask.ˍ());
            splitTask.ˏ(downloadTask.ˍ());
            splitTask.ॱ(downloadTask.ˋˋ());
            splitTask.ˎ(downloadTask.ʽॱ());
            splitTask.ˋ(downloadTask.ॱˊ());
            splitTask.ˋ(downloadTask.ॱˎ());
        }
        for (DownloadThreadInfo downloadThreadInfo : downloadTask.ˋ()) {
            SplitDownloadThreadInfo splitDownloadThreadInfo = new SplitDownloadThreadInfo();
            splitDownloadThreadInfo.ॱ(downloadThreadInfo.ˎ());
            splitDownloadThreadInfo.ˊ(downloadThreadInfo.ॱ());
            splitDownloadThreadInfo.ˎ((long) downloadTask.ʿ());
            splitDownloadThreadInfo.ˏ((long) downloadTask.ʿ());
            splitDownloadThreadInfo.ˊ(downloadThreadInfo.ˊ());
            splitDownloadThreadInfo.ˋ(downloadThreadInfo.ˋ());
            splitTask.ˎ().add(splitDownloadThreadInfo);
        }
        sessionDownloadTask.ᐝˊ().add(splitTask);
        return sessionDownloadTask;
    }
}
